import hxt from "@/utils/hxt.js"

const domain = hxt.domain;

const rateCompute = function(realFee, freeFee, handWho, handRate, serviceWho, serviceRate) {
	var data = {
		TotalFee: 0,
		PayFee: 0,
		FreeFee: freeFee * 100,
		HandFee: 0,
		ServiceFee: 0,
		RealFee: realFee * 100,
		OrderFee: realFee * 100 + freeFee * 100,
		MchFee: 0,
	};
	if (serviceWho == 1 && serviceRate > 0) data.ServiceFee = Math.round(data.RealFee * (serviceRate / 100));
	if (handWho == 1) {
		data.PayFee = Math.round((data.RealFee + data.ServiceFee) / (1 - handRate / 100));
		data.HandFee = Math.round(data.PayFee * (handRate / 100));
	} else {
		data.PayFee = data.RealFee + data.ServiceFee;
		data.HandFee = Math.round(data.PayFee * (handRate / 100));
	}
	data.TotalFee = data.PayFee + data.FreeFee;
	data.MchFee = data.PayFee - data.HandFee;
	return {
		TotalFee: (data.TotalFee / 100).toFixed(2),
		PayFee: (data.PayFee / 100).toFixed(2),
		FreeFee: (data.FreeFee / 100).toFixed(2),
		HandFee: (data.HandFee / 100).toFixed(2),
		ServiceFee: (data.ServiceFee / 100).toFixed(2),
		RealFee: (data.RealFee / 100).toFixed(2),
		OrderFee: (data.OrderFee / 100).toFixed(2),
		MchFee: (data.MchFee / 100).toFixed(2)
	};
};
//调起微信支付
const requestPayment = function(data) {
	wx.requestPayment({
		timeStamp: data.timeStamp,
		nonceStr: data.nonceStr,
		package: data.package,
		signType: data.signType,
		paySign: data.paySign,
		success: function(res) {
			hxt.isFunction(data.success) && data.success(res);
		},
		fail: function(res) {
			hxt.isFunction(data.fail) && data.fail(res);
		},
		complete: function(res) {
			hxt.isFunction(data.complete) && data.complete(res);
		}
	});
};
//前端支付状态
const clientStatus = function(param) {
	hxt.post({
		url: domain + 'wechat/payCallback',
		data: { polId: param.polId, wxMsg: param.errMsg },
		ok: param.ok,
		fail: param.fail
	})
};

module.exports = {
	rateCompute,
	requestPayment,
	clientStatus
};
